package com.shy.collect.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shy.collect.entity.UserPayConfig;
import com.shy.collect.entity.UserPayInfo;
import com.shy.collect.service.UserPayConfigService;
import com.shy.configs.common.enums.RCode;
import com.shy.configs.converts.UserPayConfigConvert;
import com.shy.configs.mvc.models.AjaxResult;
import com.shy.configs.mvc.models.PageObject;
import com.shy.configs.mvc.models.query.UserPayConfigQuery;
import com.shy.configs.mvc.models.vo.UserPayConfigVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商户支付配置
 */
@RestController
@RequestMapping("/payConfig")
public class UserPayConfigController extends BaseController {

    @Resource
    private UserPayConfigService userPayConfigService;

    @Resource
    private UserPayConfigConvert payConfigConvert;

    /**
     * 分页查询商户支付配置
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/pageList")
    public AjaxResult pageList(Integer pageNo, Integer pageSize) {
        pageNo = defPageNo(pageNo);
        pageSize = defPageSze(pageSize);
        //查询结果
        IPage<UserPayConfig> pageResult = userPayConfigService.pageList(pageNo, pageSize);
        //分页对象
        PageObject pageObject = new PageObject(pageResult);
        List<UserPayConfig> userPayConfigList = pageResult.getRecords();
        //转换为Vo
        List<UserPayConfigVo> userPayConfigVos = payConfigConvert.convertPayConfigVoList(userPayConfigList);
        return AjaxResult.success(wrapData(userPayConfigVos, pageObject));
    }

    /**
     * 添加商户支付配置
     */
    @RequestMapping("/add")
    public AjaxResult addPayConfig(@RequestBody UserPayConfigQuery userPayConfigQuery) {
        userPayConfigService.addPayConfig(userPayConfigQuery);
        return AjaxResult.success();
    }

    /**
     * 查看商户支付配置
     */
    @GetMapping("/configView")
    public AjaxResult configView(String userNo, String productCode) {
        if (StrUtil.isNotBlank(userNo) && StrUtil.isNotBlank(productCode)) {
            List<UserPayInfo> userPayConfigList = userPayConfigService.configView(userNo, productCode);
            return AjaxResult.success(userPayConfigList);
        }
        return AjaxResult.fail(RCode.REQUEST_PARAM_ERROR);
    }
    /**
     * 删除商户支付配置
     */
    @PostMapping("/delete")
    public AjaxResult deletePayConfig(String userNo, String productCode) {
        if (StrUtil.isNotBlank(userNo) && StrUtil.isNotBlank(productCode)) {
            userPayConfigService.deletePayConfig(userNo, productCode);
            return AjaxResult.success();
        }
        return AjaxResult.fail(RCode.REQUEST_PARAM_ERROR);
    }
}
